from django.contrib.auth.models import Group, Permission
from rest_framework import serializers
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.meiduo_admin.utils import CurPagination


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = "__all__"


class GroupsView(ModelViewSet):
    permission_classes = [IsAdminUser]
    pagination_class = CurPagination
    lookup_value_regex = '\d+'
    queryset = Group.objects.order_by('id')
    serializer_class = GroupSerializer

    def simple(self, request):
        """权限池"""
        data = Permission.objects.order_by('id').values('id', 'name')
        return Response(data)
